<template>
  <div id="articleFrom">
    <el-form ref="form" :model="form" label-width="160px">
      <!-- 标题 -->
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <!-- 标签分类 -->
      <el-form-item label="标签分类" prop="category_id">
        <el-select :value="form.category_id" v-model="form.category_id" placeholder="请选择活动区域">
          <el-option :label="item.name" :value="item.id" v-for="item of category" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <!-- 热门设置 -->
      <el-form-item label="热门">
        <el-switch
          v-model="form.hot"
          :active-value="1"
          :inactive-value="0"
          active-color="#02538C"
          inactive-color="#B9B9B9"
        />
      </el-form-item>
      <!-- 上传缩略图 -->
      <el-form-item label="缩略图">
        <el-upload
          action="#"
          list-type="picture-card"
          :auto-upload="false"
          :on-change="handleFileChange"
          :file-list="fileList"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                <i class="el-icon-zoom-in"></i>
              </span>
            </span>
          </div>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>
      </el-form-item>
      <!-- 内容区 -->
      <el-form-item label="内容">
        <div id="editor" ref="editorElm"></div>
      </el-form-item> 
      <!-- 按钮 -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即修改</el-button>
        <el-button @click="$router.push('/article')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import E from "wangeditor";
export default {
  name: "ArticleFrom",
  data() {
    return {
      // 默认不显示图片预览
      dialogVisible: false,
      disabled: false,
      //图片预览地址
      dialogImageUrl: "",
      category: [],
      article: {},
      editorContent:''
    };
  },
  props: {
    form: {
      type: Object,
      value: {}
    },
    fileList: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  methods: {
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleFileChange(file) {
      this.form.description_image_path = file.raw;
    },
    // 富文本设置
    handleOpen() {
      // 将原先的富文本编辑器清空，防止嵌套 BUG
      this.$refs.editorElm.innerHTML = "";
      // 实例化富文本编辑器
      this.editor = new E("#editor");
      // 当富文本器中的内容发生改变时，把内容赋值到表单数据中
      this.editor.customConfig.onchange = html => {
        this.form.content = html;
      };
      // this.editor.customConfig.menus = ["head", "bold", "italic", "underline"];
      this.editor.create();
      this.editor.txt.html(this.form.content);
    },
    // 添加或修改时间
    onSubmit() {
      this.category.forEach(e => {
        if (e.id === this.form.category_id) {
          this.form.category_name = e.name;
          return;
        }
      });
      // 将数据转换为表单数据
      let form = new FormData();
      for (let i in this.form) {
        form.append(i, this.form[i]);
      }
      // 将数据传给父组件
      this.$emit("editArticle", form);
    },
    // 获取分类
    requireCategory() {
      this.$http({
        url: "api/category"
      }).then(res => {
        this.category = res.data;
      });
    }
  },
  mounted() {
    // 挂载获取分类
    this.requireCategory();

  },
  watch:{
    // 监听父组件传递过来的数据
    form(newVal){
        // 父组件数据传递完毕后，挂载富文本
       this.handleOpen();
    }
  }
};
</script>
<style lang='less'>
#articleFrom {
  padding: 40px 100px 0 0;
  .el-form-item__label {
    font-size: 16px;
  }
}

// 图片上传样式
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>